<?php
session_start();
include 'SimpleImage.php';
include 'fungsi.php';
$kode = $_GET['id'];

if($_POST['btnsimpan']=="Batal")
{
	$baru = new fungsi();
	$page = "form-daftar-produk.php";
	$hasil=$baru->redirectPage($page);
	echo $hasil;
}
	if($_POST['btnsimpan']=="Update")
	{ 
		$nama = $_POST['nama'];
		$deskripsi = $_POST['deskripsi'];
		$isiGambar = $_POST['gambar'];
		$gambar = $_FILES['gambar']['name'];
		if($isiGambar=="")
			{
				echo"<script>alert('data harus diisi semua, termasuk gambar !')</script>";
				$baru = new fungsi();
				//header('location:editproduk.php?id='.$kode);
				$page = "editproduk.php?id=".$kode;
				$hasil=$baru->redirectPage($page);
				echo $hasil;	
			} else {
		if($nama=="" && $deskripsi=="")
		{	
				echo"<script>alert('data harus diisi semua, termasuk gambar !')</script>";
				$baru = new fungsi();
				//header('location:editproduk.php?id='.$kode);
				$page = "editproduk.php?id=".$kode;
				$hasil=$baru->redirectPage($page);
				echo $hasil;	
			
		}else
			{ 
				$baru = new fungsi();
				$baru->konek();
				
				$sql1="select gambar from produk where kode_produk=".$kode;
				
				$hasil=mysql_query($sql1);
				//var_dump($hasil);
				$image=mysql_fetch_array($hasil);
				
				@unlink($image['gambar']);
				//$result=mysql_query("delete from table_name where question_id='$id'");
				
				
				
				$image = new SimpleImage();
				$image->load($_FILES['gambar']['tmp_name']);
				$image->resize(150,150);
				$image->save("images/".$gambar);
			   
				
				$name = $_SESSION['username'];
				//$sql= "insert into produk (kode_akun,kode_produk, nama_produk, deskripsi_produk, gambar) values (' " .$name." ',' ',' ".$nama. " ', ' ".$deskripsi." ', 'images/".$gambar. " ' )";
				$sql="update produk set nama_produk=' ".$nama." ', deskripsi_produk=' ".$deskripsi." ', gambar='images/".$gambar." ' where kode_produk=' ".$kode." ' ";
				//var_dump($sql);
				mysql_query($sql);
				if(mysql_errno() == 0)
				{
	?>
				<script>alert('Data berhasil diupdate !');</script>
	<?php				
					
					$page = "form-daftar-produk.php?log=".$_SESSION['username'];
					$hasil=$baru->redirectPage($page);
					echo $hasil;
				
				
			}else{
				  echo"<script>alert('Data gagal diupdate !');</script>";
			  }
		}
	}else{

			$baru = new fungsi();
			$konek = $baru->konek();
			$sql= "SELECT * FROM produk WHERE kode_produk ='".$kode."'";  
			
			$result = mysql_query($sql);
				if(mysql_num_rows($result)>0)
				{
					while($data = mysql_fetch_array($result))
					{
					     $nama = $data['nama_produk'];
						 $deskripsi = $data['deskripsi_produk'];
					}
				}
		}
?>
<html lang="en">
<head>
	<link type="text/css" rel="stylesheet" href="css/bootstrap.css"/>
</head>
<header>  
<title>Jajanan Ngalam</title>
	<div class="hero-unit" style="margin-left:70px; margin-top:70px; margin-right:80px">
		<h1><font face="comic sans MS" color="green">JAJANAN NGALAM</font></h1>
		<p><font face="comic sans MS" size=4> Website Integrasi pedagang jajanan asli kota Malang </font></p>
	</div>
</header>
<body>
<div class="hero-unit" style="width:80%; height:auto; margin-left:70px;">
	<div class="container-fluid">
		<div class="row-fluid">
			<div class="row">
				<div class="span9">	
					<div class="navbar">
						<div class="navbar-inner" style="margin-left:20; margin-right:20">
						<a class="brand">PENJUAL</a>
							<ul class="nav">
								<li><a href="form-daftar-akun.php">Home</a></li>
								<li class="active"><a href="form-daftar-produk.php">Produk</a></li>
								<li><a href="map_penjual.php">Lokasi Jajanan</a></li>
								<li><a href="kritik_penjual.php">Kritik dan Saran</a></li>
							</ul>
						</div>
						<div class="container-fluid">
							<div class="well span12">
									<a>
											<table class="table">
													
														
															<form action="" method="post" enctype="multipart/form-data">
															
															 <tr>
																<td>Nama</td>
																<td>
																   <input type="text" name="nama" value="<?php echo"$nama"; ?>" />
																</td>
															</tr>
															<tr>
																<td>Deskripsi</td>
																<td>
																	<textarea rows="10" class="span6" name="deskripsi"><?php echo "$deskripsi"; ?></textarea>
																   <!--<input type="text" name="deskripsi" value="<?php echo"$deskripsi"; ?>"/>-->
																														 
																</td>
															</tr>
															<tr>
															<td>Gambar</td>
															<td>
															<input type="file" name="gambar">	</td>
															</tr>
															<td colspan="2" align="right">
																   <input type="submit" name="btnsimpan" value="Update" />
																   <input type="submit" name="btnsimpan" value="Batal" />
																</td> 
														 </form>
														 
															
														 
														
													
											</table>
									</a>
								
									
							
									
							</div>
							
						</div>	
					</div>	
				</div>	
				<div class="span3">
					<div class="well"></div>
				</div>
			</div>
		</div>
	</div>
</div>
	<script src="js/bootstrap.min.js"></script>
	<script src="js/bootstrap.js"></script>
	<script src="js/bootstrap-tab.js"></script>
</body>
<footer>
	<div class="hero-unit" style="margin-left:70px; margin-bottom:70px; margin-right:80px">
	</div>
</footer>
</html>
